//
//  SLSortButton.m
//  SLAppProduct
//
//  Created by Jianbo Cai on 2018/8/8.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "SLSortButton.h"
#import <JFBase/JFBase.h>

@interface SLSortButton ()

@property (nonatomic, strong) NSMutableDictionary *settingDic;

@property (nonatomic, assign) BOOL isDraw;

@end

@implementation SLSortButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self defaultSetting];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultSetting];
    }
    return self;
}

+ (instancetype)buttonWithType:(UIButtonType)buttonType {
    SLSortButton *button = [super buttonWithType:buttonType];
    [button defaultSetting];
    return button;
}

- (void)defaultSetting {
    
    [self setImage:AWThemeImageForKey(@"Kit.sort_narmal") forSortState:SLSortButtonSortStatusNormal];
    [self setImage:AWThemeImageForKey(@"Kit.sort_down") forSortState:SLSortButtonSortStatusDescending];
    [self setImage:AWThemeImageForKey(@"Kit.sort_up") forSortState:SLSortButtonSortStatusAscending];
}

- (void)drawRect:(CGRect)rect {
    [self upateTheView];
    
    self.isDraw = YES;
}

- (void)setImage:(nullable UIImage *)image forSortState:(SLSortButtonSortStatus)sortStatus {
    NSMutableDictionary *sortStatusDic = [self.settingDic objectForKey:@(sortStatus).stringValue];
    if (!sortStatusDic) {
        sortStatusDic = [NSMutableDictionary dictionaryWithCapacity:3];
        [self.settingDic setObject:sortStatusDic forKey:@(sortStatus).stringValue];
    }
    if (image) {
        [sortStatusDic setObject:image forKey:@"image"];
    }else {
        [sortStatusDic removeObjectForKey:@"image"];
    }
    
    if (self.isDraw) {
        [self upateTheView];
    }
}

- (void)setTitle:(nullable NSString *)title forSortState:(SLSortButtonSortStatus)sortStatus {
    NSMutableDictionary *sortStatusDic = [self.settingDic objectForKey:@(sortStatus).stringValue];
    if (!sortStatusDic) {
        sortStatusDic = [NSMutableDictionary dictionaryWithCapacity:3];
        [self.settingDic setObject:sortStatusDic forKey:@(sortStatus).stringValue];
    }
    if (title) {
        [sortStatusDic setObject:title forKey:@"title"];
    }else {
        [sortStatusDic removeObjectForKey:@"title"];
    }
    
    if (self.isDraw) {
        [self upateTheView];
    }
}

- (void)setTitleFont:(nullable UIFont *)titleFont forSortState:(SLSortButtonSortStatus)sortStatus {
    NSMutableDictionary *sortStatusDic = [self.settingDic objectForKey:@(sortStatus).stringValue];
    if (!sortStatusDic) {
        sortStatusDic = [NSMutableDictionary dictionaryWithCapacity:3];
        [self.settingDic setObject:sortStatusDic forKey:@(sortStatus).stringValue];
    }
    if (titleFont) {
        [sortStatusDic setObject:titleFont forKey:@"titleFont"];
    }else {
        [sortStatusDic removeObjectForKey:@"titleFont"];
    }
    
    if (self.isDraw) {
        [self upateTheView];
    }
}

- (void)setTitleColor:(nullable UIColor *)color forSortState:(SLSortButtonSortStatus)sortStatus {
    NSMutableDictionary *sortStatusDic = [self.settingDic objectForKey:@(sortStatus).stringValue];
    if (!sortStatusDic) {
        sortStatusDic = [NSMutableDictionary dictionaryWithCapacity:3];
        [self.settingDic setObject:sortStatusDic forKey:@(sortStatus).stringValue];
    }
    if (color) {
        [sortStatusDic setObject:color forKey:@"titleColor"];
    }else {
        [sortStatusDic removeObjectForKey:@"titleColor"];
    }
    
    if (self.isDraw) {
        [self upateTheView];
    }
}

- (void)upateTheView {
    NSMutableDictionary *attDic = [NSMutableDictionary dictionaryWithCapacity:4];
    id font = [self getSortStatusVaule:self.sortStatus key:@"titleFont"];
    if (font) {
        [attDic setObject:font forKey:NSFontAttributeName];
    }
    
    id titleColor = [self getSortStatusVaule:self.sortStatus key:@"titleColor"];
    if (titleColor) {
        [attDic setObject:titleColor forKey:NSForegroundColorAttributeName];
    }
    
    id title = [self getSortStatusVaule:self.sortStatus key:@"title"];
    if (!title) {
        title = @"";
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title
                                                                                         attributes:attDic];
    
    id image = [self getSortStatusVaule:self.sortStatus key:@"image"];
    if (image) {
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = image;
        attachment.bounds = CGRectMake(0, -1, attachment.image.size.width, attachment.image.size.height);
        NSAttributedString *sortImageAttributedString = [NSAttributedString attributedStringWithAttachment:attachment];
        [attributedString appendAttributedString:sortImageAttributedString];
    }
    
    [self setAttributedTitle:attributedString forState:UIControlStateNormal];
}

- (void)setSortStatus:(SLSortButtonSortStatus)sortStatus {
    _sortStatus = sortStatus;
    
    [self upateTheView];
}

- (id)getSortStatusVaule:(SLSortButtonSortStatus)sortStatus key:(NSString *)key {
    NSMutableDictionary *sortStatusDic = [self.settingDic objectForKey:@(sortStatus).stringValue];
    if (!sortStatusDic) {
        // 如果没有定义其它状态，取normal的状态
        sortStatusDic = [self.settingDic objectForKey:@(SLSortButtonSortStatusNormal).stringValue];
    }else {
        // 如果定义了其它状态，但状态中没有该值，也直接取
        if (![sortStatusDic objectForKey:key]) {
            sortStatusDic = [self.settingDic objectForKey:@(SLSortButtonSortStatusNormal).stringValue];
        }
    }
    return [sortStatusDic objectForKey:key];
}

- (NSMutableDictionary *)settingDic {
    if (!_settingDic) {
        _settingDic = [[NSMutableDictionary alloc]initWithCapacity:3];
    }
    return _settingDic;
}

@end

